package com.example.hszpad;

import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hszpad.adapter.BaseRecyclerAdapter;
import com.example.hszpad.bean.MenuItemBean;
import com.example.hszpad.service.ModbusSlaveService;
import com.example.hszpad.util.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

public class HSZMainActivity extends AppCompatActivity implements View.OnClickListener, ModbusSlaveService.onReceiveListener {
    private static final String TAG = "HSZMainActivity";
    //    private List<Fragment> fragmentList = new ArrayList<>();
//    private ViewPager viewPager;
//    private MyFragmentAdapter adapter = new MyFragmentAdapter(getSupportFragmentManager());
    private RecyclerView recyclerView;
    private RecyclerView recyclerViewChild;
    private BaseRecyclerAdapter<MenuItemBean> recyclerAdapter;
    private BaseRecyclerAdapter<MenuItemBean> recyclerChildAdapter;
    private List<MenuItemBean> menuItemBeanList = new ArrayList<>();
    private List<MenuItemBean> menuItemBeanChildList = new ArrayList<>();

    private ModbusSlaveService modbusSlaveService;
    private ServiceConnection serviceConnection;

    private LocalBroadcastManager localBroadcastManager;
    //    private LocalReceiver localReceiver;
    private IntentFilter intentFilter;

    private MenuItemBean curItemBean;

    private MenuItemBean curChildBean;


//    private LinearLayout ll_01;
//    private LinearLayout ll_02;
//    private LinearLayout ll_03;
//    private LinearLayout ll_04;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ScreenUtil.hideBottomNav(this);
//        viewPager = findViewById(R.id.viewpager);
//        ll_01 = findViewById(R.id.ll_01);
//        ll_02 = findViewById(R.id.ll_02);
//        ll_03 = findViewById(R.id.ll_03);
//        ll_04 = findViewById(R.id.ll_04);
//        ll_01.setOnClickListener(this);
//        ll_02.setOnClickListener(this);
//        ll_03.setOnClickListener(this);
//        ll_04.setOnClickListener(this);
//        fragmentList.add(new HomeFragment());
//        fragmentList.add(new BdcsFragment());
//        fragmentList.add(new IShipFragment());
//        fragmentList.add(new OtherFragment());
//        viewPager.setAdapter(adapter);
//        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//                setOnClick(position);
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });
//        setOnClick(0);
//        recyclerView = findViewById(R.id.recyclerView);
//        recyclerViewChild = findViewById(R.id.recyclerViewChild);

//        GridLayoutManager gridLayoutManager2 = new GridLayoutManager(this, 5);
//        recyclerViewChild.setLayoutManager(gridLayoutManager2);
//        menuItemBeanList = getMenus();
//        recyclerAdapter = new BaseRecyclerAdapter<MenuItemBean>(menuItemBeanList, R.layout.layout_first_menu_item, new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                MenuItemBean itemBean = menuItemBeanList.get(position);
//                if (itemBean.isSelect()) {
//                    itemBean.setSelect(false);
//                    curItemBean = null;
//                } else {
//                    for (int i = 0; i < menuItemBeanList.size(); i++) {
//                        menuItemBeanList.get(i).setSelect(false);
//                    }
//                    itemBean.setSelect(true);
//                    curItemBean = itemBean;
//                    modbusSlaveService.setValue(itemBean.getAddress(), itemBean.getId());
//                }
//                recyclerAdapter.notifyDataSetChanged();
//                recyclerAdapter.notifyListDataSetChanged();
//                //加载子菜单
//                if (itemBean.isSelect()) {
//                    menuItemBeanChildList.clear();
//                    menuItemBeanChildList.addAll(getChildMenus(itemBean));
//                    recyclerChildAdapter.refresh(menuItemBeanChildList);
//                    if (!menuItemBeanChildList.isEmpty()) {
//                        modbusSlaveService.setValue(menuItemBeanChildList.get(0).getAddress(), menuItemBeanChildList.get(0).getId());
//                    }
//                } else {
//                    menuItemBeanChildList.clear();
//                    recyclerChildAdapter.refresh(menuItemBeanChildList);
//                }
//            }
//        }) {
//            @Override
//            protected void onBindViewHolder(SmartViewHolder holder, MenuItemBean model, int position) {
//                holder.image(R.id.iv_icon, model.getIcon());
//                holder.text(R.id.tv_title, model.getName());
//                if (model.isSelect()) {
//                    holder.viewBgColorId(R.id.ll_view, R.color.colorPrimaryDark);
//                } else {
//                    holder.viewBgColorId(R.id.ll_view, R.color.colorPrimary);
//                }
//            }
//        };
//        recyclerChildAdapter = new BaseRecyclerAdapter<MenuItemBean>(menuItemBeanChildList, R.layout.layout_first_menu_item, new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                MenuItemBean itemBean = menuItemBeanChildList.get(position);
//                if (itemBean.isSelect()) {
//                    itemBean.setSelect(false);
//                    curChildBean = null;
//                } else {
//                    for (int i = 0; i < menuItemBeanChildList.size(); i++) {
//                        menuItemBeanChildList.get(i).setSelect(false);
//                    }
//                    itemBean.setSelect(true);
//                    curChildBean = itemBean;
//                    modbusSlaveService.setValue(itemBean.getParent().getAddress(), itemBean.getParent().getId());
//                    modbusSlaveService.setValue(itemBean.getAddress(), itemBean.getId());
//                }
//                recyclerChildAdapter.notifyDataSetChanged();
//                recyclerChildAdapter.notifyListDataSetChanged();
//            }
//        }) {
//            @Override
//            protected void onBindViewHolder(SmartViewHolder holder, MenuItemBean model, int position) {
//                holder.image(R.id.iv_icon, model.getIcon());
//                holder.text(R.id.tv_title, model.getName());
//                if (model.isSelect()) {
//                    holder.viewBgColorId(R.id.ll_view, R.color.colorAccentDark);
//                } else {
//                    holder.viewBgColorId(R.id.ll_view, R.color.colorAccent);
//                }
//            }
//        };
//        recyclerView.setAdapter(recyclerAdapter);
//        recyclerViewChild.setAdapter(recyclerChildAdapter);
////        recyclerView.addItemDecoration(new DividerGridItemDecoration(this));
//        serviceConnection = new ServiceConnection() {
//            @Override
//            public void onServiceConnected(ComponentName name, IBinder service) {
//                ModbusSlaveService.LocalBinder binder = (ModbusSlaveService.LocalBinder) service;
//                modbusSlaveService = binder.getServices();
//                modbusSlaveService.myway();
//                modbusSlaveService.start();
//                modbusSlaveService.setOnReceiveListener(HSZMainActivity.this);
//            }
//
//            @Override
//            public void onServiceDisconnected(ComponentName name) {
//                modbusSlaveService.setOnReceiveListener(null);
//                modbusSlaveService.stop();
//                modbusSlaveService = null;
//                Log.e(TAG, "onServiceDisconnected:");
//
//            }
//        };
//        //绑定服务
//        Intent intent = new Intent(this, ModbusSlaveService.class);
//        bindService(intent, serviceConnection, Service.BIND_AUTO_CREATE);
//        localBroadcastManager = LocalBroadcastManager.getInstance(this);
//        intentFilter = new IntentFilter();
//        intent.setAction(ModbusSlaveService.BROADCAST_ONWRITETOMULTIPLECOILS);
//        intent.setAction(ModbusSlaveService.BROADCAST_ONWRITETOMULTIPLEHOLDINGREGISTERS);
//        intent.setAction(ModbusSlaveService.BROADCAST_ONWRITETOSINGLECOIL);
//        intent.setAction(ModbusSlaveService.BROADCAST_ONWRITETOSINGLEHOLDINGREGISTER);
//        localReceiver = new LocalReceiver();
//        localBroadcastManager.registerReceiver(localReceiver, intentFilter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.ll_01:
//                setOnClick(0);
//                break;
//            case R.id.ll_02:
//                setOnClick(1);
//                break;
//            case R.id.ll_03:
//                setOnClick(2);
//                break;
//            case R.id.ll_04:
//                setOnClick(3);
//                break;
        }
    }

//    public void setOnClick(int position) {
//        ll_01.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
//        ll_02.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
//        ll_03.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
//        ll_04.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
//        switch (position) {
//            case 0:
//                ll_01.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
//                break;
//            case 1:
//                ll_02.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
//                break;
//            case 2:
//                ll_03.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
//                break;
//            case 3:
//                ll_04.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
//                break;
//
//        }
//        viewPager.setCurrentItem(position);
//    }
//
//    class MyFragmentAdapter extends FragmentPagerAdapter {
//        public MyFragmentAdapter(FragmentManager fm) {
//            super(fm);
//        }
//
//        @Override
//        public Fragment getItem(int position) {
//            return fragmentList.get(position);
//        }
//
//        @Override
//        public int getCount() {
//            return fragmentList.size();
//        }
//    }

    private List<MenuItemBean> getMenus() {
        List<MenuItemBean> menuItemBeanList = new ArrayList<>();
        MenuItemBean bean = new MenuItemBean(1, "动力系统", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        bean = new MenuItemBean(2, "电力系统", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        bean = new MenuItemBean(3, "辅助设备", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        bean = new MenuItemBean(6, "破损抗沉管理", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        bean = new MenuItemBean(8, "实时数据", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        bean = new MenuItemBean(9, "历史数据", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        bean = new MenuItemBean(10, "报警列表", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        return menuItemBeanList;
    }

    @Override
    protected void onDestroy() {
        unbindService(serviceConnection);
//        localBroadcastManager.unregisterReceiver(localReceiver);
        super.onDestroy();

    }

    private List<MenuItemBean> getChildMenus(MenuItemBean menuItemBean) {
        List<MenuItemBean> menuItemBeanList = new ArrayList<>();
        MenuItemBean bean = null;
        switch (menuItemBean.getId()) {
            case 1:
                bean = new MenuItemBean(menuItemBean, 1, "柴油机系统", 1, R.drawable.ic_home_white_24dp, true);
                menuItemBeanList.add(bean);
                bean = new MenuItemBean(menuItemBean, 2, "轴系/齿轮箱", 1, R.drawable.ic_home_white_24dp, false);
                menuItemBeanList.add(bean);
                break;
            case 2:
                bean = new MenuItemBean(menuItemBean, 1, "电站", 2, R.drawable.ic_home_white_24dp, true);
                menuItemBeanList.add(bean);
                bean = new MenuItemBean(menuItemBean, 2, "配电", 2, R.drawable.ic_home_white_24dp, false);
                menuItemBeanList.add(bean);
                break;
            case 3:
                bean = new MenuItemBean(menuItemBean, 1, "燃油输送", 3, R.drawable.ic_home_white_24dp, true);
                menuItemBeanList.add(bean);
                bean = new MenuItemBean(menuItemBean, 2, "滑油循环", 3, R.drawable.ic_home_white_24dp, false);
                menuItemBeanList.add(bean);
                bean = new MenuItemBean(menuItemBean, 3, "冷却系统", 3, R.drawable.ic_home_white_24dp, false);
                menuItemBeanList.add(bean);
                bean = new MenuItemBean(menuItemBean, 4, "阀门遥控", 3, R.drawable.ic_home_white_24dp, false);
                menuItemBeanList.add(bean);
                bean = new MenuItemBean(menuItemBean, 5, "压载水", 3, R.drawable.ic_home_white_24dp, false);
                menuItemBeanList.add(bean);
                bean = new MenuItemBean(menuItemBean, 6, "生活保障装备", 3, R.drawable.ic_home_white_24dp, false);
                menuItemBeanList.add(bean);
                break;
            case 6:
                bean = new MenuItemBean(menuItemBean, 1, "破损管理", 4, R.drawable.ic_home_white_24dp, true);
                menuItemBeanList.add(bean);
                bean = new MenuItemBean(menuItemBean, 2, "事件回放", 4, R.drawable.ic_home_white_24dp, false);
                menuItemBeanList.add(bean);
                break;
        }
        return menuItemBeanList;
    }

    @Override
    public void onRecevie(int address, int value) {

        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (address == 1) {
                    //第一级
                    if (curItemBean != null) {
                        if (curItemBean.getId() == value) {
                            return;
                        }
                        setSelect(value);
                    } else {
                        setSelect(value);
                    }
                } else {
                    if (curChildBean != null) {
                        if (curChildBean.getId() == value) {
                            return;
                        }
                        setSelectChild(value);
                    } else {
                        setSelectChild(value);
                    }
                }
            }
        });
    }

    @Override
    public void onReceiveMultiple(int address, int quantity, int[] values) {
        if (quantity == 1) {
            this.onRecevie(address + 1, values[0]);
        }
    }

//    class LocalReceiver extends BroadcastReceiver {
//
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            switch (intent.getAction()) {
//                case ModbusSlaveService.BROADCAST_ONWRITETOMULTIPLECOILS:
//
//                case ModbusSlaveService.BROADCAST_ONWRITETOMULTIPLEHOLDINGREGISTERS:
//
//                    break;
////                case ModbusSlaveService.BROADCAST_ONWRITETOSINGLECOIL:
////
////                    break;
//                case ModbusSlaveService.BROADCAST_ONWRITETOSINGLEHOLDINGREGISTER:
//                    int address = intent.getIntExtra("address", 0);
//                    int value = intent.getIntExtra("value", 0);
//                    if (address == 1) {
//
//                    } else {
//
//                    }
//                    Toast.makeText(HSZMainActivity.this, "主屏反馈 地址：" + address + "----值：" + value, Toast.LENGTH_SHORT).show();
//                    break;
//            }
//        }
//    }

    private void setSelect(int id) {

        for (int i = 0; i < menuItemBeanList.size(); i++) {
            if (menuItemBeanList.get(i).getId() == id) {
                menuItemBeanList.get(i).setSelect(true);
                curItemBean = menuItemBeanList.get(i);
            } else {
                menuItemBeanList.get(i).setSelect(false);
            }
        }
        recyclerAdapter.notifyDataSetChanged();
        recyclerAdapter.notifyListDataSetChanged();
        menuItemBeanChildList.clear();
        if (curItemBean != null) {
            menuItemBeanChildList.addAll(getChildMenus(curItemBean));
        }
        recyclerChildAdapter.refresh(menuItemBeanChildList);
    }

    private void setSelectChild(int id) {

        for (int i = 0; i < menuItemBeanChildList.size(); i++) {
            if (menuItemBeanChildList.get(i).getId() == id) {
                menuItemBeanChildList.get(i).setSelect(true);
                curChildBean = menuItemBeanChildList.get(i);
            } else {
                menuItemBeanChildList.get(i).setSelect(false);
            }
        }
        recyclerChildAdapter.notifyDataSetChanged();
        recyclerChildAdapter.notifyListDataSetChanged();
    }
}
